package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"weixin/models/mysql"

	"weixin/weixinrpc/internal/svc"
	"weixin/weixinrpc/weixinrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type UserUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewUserUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UserUpdateLogic {
	return &UserUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *UserUpdateLogic) UserUpdate(in *weixinrpc.UserUpdateRequest) (*weixinrpc.UserUpdateResponse, error) {
	// todo: add your logic here and delete this line

	var user mysql.User
	mysql.DB.Debug().Where("id = ?", user.ID).Find(&user)
	if user.ID == 0 {
		return nil, fmt.Errorf("用户不存在,请重新输入")
	}
	m := mysql.User{
		Model:     gorm.Model{ID: uint(in.Id)},
		Nickname:  in.Nickname,
		Name:      in.Name,
		HeadImage: in.HeadImage,
		Tel:       in.Tel,
		Uptime:    in.Uptime,
		Price:     float64(in.Price),
	}
	err := mysql.DB.Debug().Updates(&m).Error
	if err != nil {
		return nil, fmt.Errorf("数据获取修改")
	}
	return &weixinrpc.UserUpdateResponse{
		Id: int64(m.ID),
	}, nil
}
